package cn.tansanqinger.service.account.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.account.UserAccountSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.ChatMessageSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.ReportUserAndGroupSqlDaoImpl;
import cn.tansanqinger.dao.sql.topic.CommentTopicSqlDaoImpl;
import cn.tansanqinger.pojo.Chatmessage;
import cn.tansanqinger.pojo.Commenttopic;
import cn.tansanqinger.pojo.Reportuser;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.account.IUserAccountService;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class UserAccountServiceImpl implements IUserAccountService{
	
	@Autowired
	private UserAccountSqlDaoImpl userImpl;
	@Autowired
	private ReportUserAndGroupSqlDaoImpl rimpl;
	@Autowired
	private ChatMessageSqlDaoImpl cimpl;
	@Autowired
	private CommentTopicSqlDaoImpl ctimpl;

	@Override
	public PageData selectScreenAccount(PageData pageData, int jurisdiction) {
		//用于账号管理中的时间选择
		String time6 = pageData.getString("time6");
		//另外页面的开始时间输入
		String starttime = pageData.getString("starttime");
		//另外页面的结束时间输入
		String endtime = pageData.getString("endtime");
		String reg = "";
		if(null != time6 && !"".equals(time6)) {
			reg = "\\d{4}\\-\\d{2}\\-\\d{2}\\ - \\d{4}\\-\\d{2}\\-\\d{2}";
			if(time6.matches(reg)) {
				String timeSplit[] = time6.split(" - ");
				starttime = timeSplit[0].trim();
				endtime = timeSplit[1].trim();
			}
		}
		reg = "\\d{4}\\-\\d{2}\\-\\d{2}";
		PageData resultPd = userImpl.selectScreenAccount(pageData,jurisdiction);
		@SuppressWarnings("unchecked")
		List<User> list = (List<User>) resultPd.get("data");
		List<User> result = new ArrayList<User>();
		if(null != starttime && !"".equals(starttime) && endtime != null && !"".equals(endtime)){
			if(starttime.matches(reg)&&endtime.matches(reg)) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try{
					Long time1 = format.parse(starttime).getTime();
					Long time2 = format.parse(endtime).getTime();
					for (int i = 0; i < list.size(); i++) {
						Long time3 = format1.parse(list.get(i).getCreatetime()).getTime();
						if(time1<time3&&time2>time3) {
							result.add(list.get(i));
						}
					}
					resultPd.put("data", result);
				} catch (Exception e) {}
			}
		}
		return resultPd;
	}
	
	@Override
	public PageData selectTeacherAccount(PageData pageData,int dispose) {
		//用于账号管理中的时间选择
		String time6 = pageData.getString("time6");
		//另外页面的开始时间输入
		String starttime = pageData.getString("starttime");
		//另外页面的结束时间输入
		String endtime = pageData.getString("endtime");
		String reg = "";
		if(null != time6 && !"".equals(time6)) {
			reg = "\\d{4}\\-\\d{2}\\-\\d{2}\\ - \\d{4}\\-\\d{2}\\-\\d{2}";
			if(time6.matches(reg)) {
				String timeSplit[] = time6.split(" - ");
				starttime = timeSplit[0].trim();
				endtime = timeSplit[1].trim();
			}
		}
		reg = "\\d{4}\\-\\d{2}\\-\\d{2}";
		PageData resultPd = userImpl.selectTeacherAccount(pageData,dispose);
		@SuppressWarnings("unchecked")
		List<User> list = (List<User>) resultPd.get("data");
		List<User> result = new ArrayList<User>();
		if(null != starttime && !"".equals(starttime) && endtime != null && !"".equals(endtime)){
			if(starttime.matches(reg)&&endtime.matches(reg)) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try{
					Long time1 = format.parse(starttime).getTime();
					Long time2 = format.parse(endtime).getTime();
					for (int i = 0; i < list.size(); i++) {
						Long time3 = format1.parse(list.get(i).getCreatetime()).getTime();
						if(time1<time3&&time2>time3) {
							result.add(list.get(i));
						}
					}
					resultPd.put("data", result);
				} catch (Exception e) {}
			}
		}
		return resultPd;
	}

	@Override
	public User selectId(PageData pageData) {
		if(!"".equals(Tools.isEmpty(pageData.get("id")))) {
			return userImpl.selectId(Integer.parseInt(Tools.isEmpty(pageData.get("id"))));
		}
		return null;
	}

	@Override
	public PageData modificationBannedId(PageData pageData) {
		User user = new User();
		user = selectId(pageData);
		if(user!=null) {
			if(userImpl.updateBannedId(user)==1) {
				pageData.put("code","0");
				pageData.put("msg", "修改成功");
			}
		} else {
			pageData.put("code","-1");
			pageData.put("msg", "修改失败");
		}
		return pageData;
	}

	@Override
	public PageData ratify(HttpServletRequest request) {
		PageData pageData = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			User user = userImpl.selectId(Integer.parseInt(id));
			if(user!=null) {
				user.setIsdispose(1);
				user.setIsactivate(1);
				if(userImpl.updateBannedId(user)==1) {
					pageData.put("code","0");
					pageData.put("msg", "处理成功");
				} else {
					pageData.put("code","-1");
					pageData.put("msg", "处理失败");
				}
			} else {
				pageData.put("code","-1");
				pageData.put("msg", "数据异常");
			}
		} else {
			pageData.put("code","-1");
			pageData.put("msg", "数据异常");
		}
		return pageData;
	}

	@Override
	public PageData unratify(HttpServletRequest request) {
		PageData pageData = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			User user = userImpl.selectId(Integer.parseInt(id));
			if(user!=null) {
				user.setIsdispose(1);
				user.setIsreject(1);
				user.setRejectmessage(request.getParameter("message"));
				if(userImpl.updateBannedId(user)==1) {
					pageData.put("code","0");
					pageData.put("msg", "处理成功");
				} else {
					pageData.put("code","-1");
					pageData.put("msg", "处理失败");
				}
			} else {
				pageData.put("code","-1");
				pageData.put("msg", "数据异常");
			}
		} else {
			pageData.put("code","-1");
			pageData.put("msg", "数据异常");
		}
		return pageData;
	}

	@Override
	public PageData title(HttpServletRequest request) {
		PageData pageData = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			User user = userImpl.selectId(Integer.parseInt(id));
			if(user!=null) {
				user.setIsbanned(1);
				user.setIsreport(0);
				String message = Tools.isEmpty(request.getParameter("message"));
				List<Reportuser> reportusers = rimpl.selectReport(Integer.parseInt(id), 0);
				for (int i = 0; i < reportusers.size(); i++) {
					reportusers.get(i).setRisdispose(1);
					reportusers.get(i).setRdisposemessage("".equals(message)?"管理员已经将该账号封禁了，感谢您的举报，为互联网环境的稳定做出贡献。":message);
					rimpl.modification(reportusers.get(i));
				}
				if(userImpl.updateBannedId(user)==1) {
					pageData.put("code","0");
					pageData.put("msg", "处理成功");
				} else {
					pageData.put("code","-1");
					pageData.put("msg", "处理失败");
				}
			} else {
				pageData.put("code","-1");
				pageData.put("msg", "数据异常");
			}
		} else {
			pageData.put("code","-1");
			pageData.put("msg", "数据异常");
		}
		return pageData;
	}

	@Override
	public PageData deblocking(HttpServletRequest request) {
		PageData pageData = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			User user = userImpl.selectId(Integer.parseInt(id));
			if(user!=null) {
				user.setIsbanned(0);
				user.setIsstate(0);
				if(userImpl.updateBannedId(user)==1) {
					pageData.put("code","0");
					pageData.put("msg", "处理成功");
				} else {
					pageData.put("code","-1");
					pageData.put("msg", "处理失败");
				}
			} else {
				pageData.put("code","-1");
				pageData.put("msg", "数据异常");
			}
		} else {
			pageData.put("code","-1");
			pageData.put("msg", "数据异常");
		}
		return pageData;
	}

	@Override
	public PageData neglect(HttpServletRequest request) {
		PageData pageData = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			User user = userImpl.selectId(Integer.parseInt(id));
			if(user!=null) {
				user.setIsreport(0);
				user.setIsstate(0);
				List<Reportuser> reportusers = rimpl.selectReport(Integer.parseInt(id), 0);
				for (int i = 0; i < reportusers.size(); i++) {
					rimpl.delete(reportusers.get(i).getRid());
				}
				if(userImpl.updateBannedId(user)==1) {
					pageData.put("code","0");
					pageData.put("msg", "处理成功");
				} else {
					pageData.put("code","-1");
					pageData.put("msg", "处理失败");
				}
			} else {
				pageData.put("code","-1");
				pageData.put("msg", "数据异常");
			}
		} else {
			pageData.put("code","-1");
			pageData.put("msg", "数据异常");
		}
		return pageData;
	}

	@Override
	public PageData reset(HttpServletRequest request) {
		String account = Tools.isEmpty(request.getParameter("account"));
		String id = Tools.isEmpty(request.getParameter("id"));
		PageData pageData = new PageData();
		if(id.matches("\\d+")&&account.matches("\\d+")) {
			List<Chatmessage> resultList = cimpl.selectChatMessageAll(account);
			List<Commenttopic> commenttopics = ctimpl.selectCommentTopic(Integer.parseInt(id));
			pageData.put("code","0");
			pageData.put("msg", "处理成功");
			pageData.put("resultList", resultList);
			pageData.put("commenttopics", commenttopics);
		} else {
			pageData.put("code","-1");
			pageData.put("msg", "数据异常");
		}
		return pageData;
	}
}
